package cn.demoncat.util.lang.entity;

import java.io.Serializable;
import java.util.Objects;

/**
 * 分页参数
 * 
 * @author 延晓磊
 *
 * @since 2018年8月31日
 */
public class PageParam implements Serializable {

	private static final long serialVersionUID = 1L;

	// 页号
	private Integer pageNum;
	// 页长
	private Integer pageSize;
	// 页偏移
	private Long pageStart;

	/**
	 * 构建分页参数
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2018年8月31日
	 */
	public static PageParam build(Integer pageNum, Integer pageSize) {
		// 页长
		if (pageSize == null || pageSize < 1) {
			pageSize = Pager.DEFAULT_PAGE_SIZE;
		} else if (pageSize > Pager.MAX_PAGE_SIZE) {
			pageSize = Pager.MAX_PAGE_SIZE;
		}
		// 页码
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}
		return new PageParam(pageNum, pageSize);
	}

	public PageParam(Integer pageNum, Integer pageSize) {
		super();
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.pageStart = Pager.getPageStart(pageNum, pageSize);
	}

	public PageParam() {
		super();
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public long getPageStart() {
		return pageStart;
	}

	public void setPageStart(Long pageStart) {
		this.pageStart = pageStart;
	}

	@Override
	public String toString() {
		return "PageParam [pageNum=" + pageNum + ", pageSize=" + pageSize + ", pageStart=" + pageStart + "]";
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		PageParam pageParam = (PageParam) o;
		return Objects.equals(pageNum, pageParam.pageNum) &&
				Objects.equals(pageSize, pageParam.pageSize) &&
				Objects.equals(pageStart, pageParam.pageStart);
	}

	@Override
	public int hashCode() {
		return Objects.hash(pageNum, pageSize, pageStart);
	}
}
